/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.managedbean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import mrp.productionPlanning.entity.DailyDetailEntity;
import mrp.productionPlanning.entity.DailyMPSTableEntity;
import mrp.productionPlanning.entity.MPSTableEntity;
import mrp.productionPlanning.entity.MonthlyDetailEntity;
import mrp.productionPlanning.session.DailyMPSTableSessionBean;
import mrp.productionPlanning.session.MPSTableSessionBean;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class CreateDailyMPSTableManagedBean {

    private int year;
    private String month;
    @EJB
    DailyMPSTableSessionBean dailyMPSTableSessionBean;
    @EJB
    MPSTableSessionBean mPSTableSessionBean;

    /** Creates a new instance of CreateDailyMPSTableManagedBean */
    public CreateDailyMPSTableManagedBean() {
    }

    public void CreateDailyMPSTable() throws IOException {
        List<DailyMPSTableEntity> dailyMpsListA = new ArrayList<DailyMPSTableEntity>();
        List<DailyMPSTableEntity> dailyMpsListB = new ArrayList<DailyMPSTableEntity>();
        List<DailyMPSTableEntity> dailyMpsListC = new ArrayList<DailyMPSTableEntity>();
        List<DailyMPSTableEntity> dailyMpsListD = new ArrayList<DailyMPSTableEntity>();
        
        MPSTableEntity mPSTableEntityA = mPSTableSessionBean.getMPSTableEntityByMonth(month, "productA", year);
        MonthlyDetailEntity monthlyDetailEntityA = mPSTableEntityA.getMonthlyDetailEntity();
        List<DailyDetailEntity> workingDays = monthlyDetailEntityA.getDays();
        workingDays = dailyMPSTableSessionBean.sortList(workingDays);
        for (int j = 0; j < workingDays.size(); j++) {
            System.out.println("Sort List......." + workingDays.get(j).getDate().toString());
        }

        int i = 0;
        System.out.println("Test1****************" + workingDays.size());
        while (i < workingDays.size()) {
            Date selectedDate = workingDays.get(i).getDate();
            Calendar c = Calendar.getInstance();
            c.setTime(selectedDate);
//            selectedDate = c.getTime();
            System.out.println("Test2******************" + selectedDate.toString());

            dailyMpsListA.add(dailyMPSTableSessionBean.createMPSTableA(c));
            dailyMpsListB.add(dailyMPSTableSessionBean.createMPSTableB(c));
            dailyMpsListC.add(dailyMPSTableSessionBean.createMPSTableC(c));
            dailyMpsListD.add(dailyMPSTableSessionBean.createMPSTableD(c));
            i++;
        }
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dailyMpsListA", dailyMpsListA);
        System.out.println("Temp Test1......." + dailyMpsListA.size());
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dailyMpsListB", dailyMpsListB);
        System.out.println("Temp Test2......." + dailyMpsListB.size());
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dailyMpsListC", dailyMpsListC);
        System.out.println("Temp Test3......." + dailyMpsListC.size());
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dailyMpsListD", dailyMpsListD);
        System.out.println("Temp Test4......." + dailyMpsListD.size());
        System.out.println("---------------------------------------End Of Generation---------------------------------------");
        FacesContext.getCurrentInstance().getExternalContext().redirect("editDailyMPSTable.xhtml");
    }
//    private int convertMonth(String in){
//          int out = 0;
//        if (in.equalsIgnoreCase("Jan")) {
//            out = 1;
//        } else if (in.equalsIgnoreCase("Feb")) {
//            out = 2;
//        } else if (in.equalsIgnoreCase("Mar")) {
//            out = 3;
//        } else if (in.equalsIgnoreCase("Apr")) {
//            out = 4;
//        } else if (in.equalsIgnoreCase("May")) {
//            out = 5;
//        } else if (in.equalsIgnoreCase("Jun")) {
//            out = 6;
//        } else if (in.equalsIgnoreCase("Jul")) {
//            out = 7;
//        } else if (in.equalsIgnoreCase("Aug")) {
//            out = 8;
//        } else if (in.equalsIgnoreCase("Sep")) {
//            out = 9;
//        } else if (in.equalsIgnoreCase("Oct")) {
//            out = 10;
//        } else if (in.equalsIgnoreCase("Nov")) {
//            out = 11;
//        } else if (in.equalsIgnoreCase("Dec")) {
//            out = 12;
//        }
//        return out;
//    }   

    public void jump() throws IOException {
        List<DailyMPSTableEntity> dailyMpsListA = new ArrayList<DailyMPSTableEntity>();
        List<DailyMPSTableEntity> dailyMpsListB = new ArrayList<DailyMPSTableEntity>();
        List<DailyMPSTableEntity> dailyMpsListC = new ArrayList<DailyMPSTableEntity>();
        List<DailyMPSTableEntity> dailyMpsListD = new ArrayList<DailyMPSTableEntity>();
//        SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
//        Calendar cal = new GregorianCalendar();
//        cal.set(yearName, 0, 1, 0 , 1, 0);
//        Date d = new Date();
//        d = cal.getTime();
//        int correction = cal.get(Calendar.MONTH);
//        String temp = sdf.format(cal.getTime());
//        try{
//            d = sdf.parse(temp);
//        }
//        catch(ParseException ex){
//        
//        }
//        cal.setTime(d);
//        cal.set(Calendar.MONTH, correction);
//        d = cal.getTime();
//        System.out.println("Date require:" + d.toString());
  
        dailyMpsListA = dailyMPSTableSessionBean.getAllDailyMPSTableEntityByMonth("productA", month, year);
        dailyMpsListB = dailyMPSTableSessionBean.getAllDailyMPSTableEntityByMonth("productB", month, year);
        dailyMpsListC = dailyMPSTableSessionBean.getAllDailyMPSTableEntityByMonth("productC", month, year);
        dailyMpsListD = dailyMPSTableSessionBean.getAllDailyMPSTableEntityByMonth("productD", month, year);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dailyMpsListA", dailyMpsListA);
//        System.out.println("Temp Test1......." + dailyMpsListA.size());
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dailyMpsListB", dailyMpsListB);
//        System.out.println("Temp Test1......." + dailyMpsListB.size());
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dailyMpsListC", dailyMpsListC);
//        System.out.println("Temp Test1......." + dailyMpsListC.size());
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dailyMpsListD", dailyMpsListD);
//
//        System.out.println("Temp Test1......." + dailyMpsListD.size());
        FacesContext.getCurrentInstance().getExternalContext().redirect("editDailyMPSTable.xhtml");
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }
}
